#!perl

use strict;
use Text::CSV;

my $out = $ARGV[0];
my $csv = $ARGV[1];
my $key = $ARGV[2];
my $csv_object = Text::CSV->new({binary=>1,});
my %map;

open(DEVICE_OUT, "<$out");
open(DEVICE_CSV, ">$csv")or die "input file open fail";
while (<DEVICE_OUT>)
{
	chomp $_;
	my $status = $csv_object->parse($_);
	my @columns = $csv_object->fields();
	my $value = $columns[$key];
	if (exists($map{$value}))
	{
		next;
	}
	if ($value eq "")
	{
		next;
	}
	if ($value eq "NULL")
	{
		next;
	}
	$_ =~ s/NULL//g;
	$map{$value} = 1;
	print DEVICE_CSV $_;
	print DEVICE_CSV "\n";
}
close(DEVICE_CSV);
close(DEVICE_OUT);
